package com.ybg.engine.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 系统配置Holder
 * 
 * @author lishiwei
 * @data 2017年5月24日
 *
 */

public class SystemConfigHolder {

	private SystemConfigDao dao;

	private Map<String, Object> configs;

	public SystemConfigHolder(JdbcTemplate jdbcTemplate) {
		dao = new SystemConfigDao(jdbcTemplate);
	}

	public Map<String, Object> getConfigs() {
		if (configs == null) {
			synchronized (SystemConfigHolder.class) {
				if (configs == null) {
					loadConfigs();
				}
			}
		}
		return configs;
	}

	public Object getValue(String key) {
		return getConfigs().get(key);
	}

	public String getStringValue(String key) {
		return String.valueOf(getValue(key));
	}

	private void loadConfigs() {
		configs = new HashMap<String, Object>();
		List<Map<String, Object>> list = dao.query();
		for (Map<String, Object> map : list) {
			configs.put(map.get("paramkey").toString(), map.get("paramvalue"));
		}
	}
}
